import { getStationNames, getAlarmPageInfos } from "@/api/alarmInfo"
import dayjs from 'dayjs'

export default {
  data() {
    return {
      alarmInfo: {
        obj: {},
        // obj: {
          station: '', // 站场名称
          trainNo: '', // 机车号
          startTime: new Date(new Date().getTime() - 3600 * 1000 * 24),
          endTime: new Date(new Date().getTime()),
        // },
        //workNo: '',   // 作业单号
        page: 1,
        size: 10
      },
      listLoading: true,
      tableKey: 0,
      currentPage: 1,
      pageSize: 10,
      totalDataLength: 0,
      stationNameOptions: [],
      rcvTableData: [],
      tableHeight: 0
    }
  },
  created() {
    this.getAllStationNames()
    this.getAllTrainInfos()
  },
  mounted() {
    this.$nextTick(() => {
      // 根据浏览器高度设置初始高度
      this.tableHeight = window.innerHeight - this.$refs.refsTable.$el.offsetTop - 150
      // 监听浏览器高度变化，修改表格高度
      window.onresize = () => {
        this.tableHeight = window.innerHeight - this.$refs.refsTable.$el.offsetTop - 150
      }
      // this.alarmInfo.obj.startTime = this.formateTime(new Date(new Date().getTime() - 3600 * 1000 * 24))
      // this.alarmInfo.obj.endTime = this.formateTime(new Date(new Date().getTime()))
    })
  },
  computed: {},
  watch: {
    startTime: function(newTime, oldTime) {
      if (!newTime) {
        this.startTime = new Date(new Date().getTime() - 3600 * 1000 * 24)
      }
    },
    endTime: function(newTime, oldTime) {
      if (!newTime) {
        this.endTime = new Date(new Date().getTime())
      }
    }
  },
  methods: {
    formateTime(time) {
      return dayjs(time).format('YYYY-MM-DD HH:mm:ss')
    },
    async getAllTrainInfos() {
      this.listLoading = true
      this.globalTableData = []
      this.alarmInfo.obj.startTime = this.formateTime(this.alarmInfo.startTime)
      this.alarmInfo.obj.endTime = this.formateTime(this.alarmInfo.endTime)
      this.alarmInfo.obj.station = this.alarmInfo.station
      this.alarmInfo.obj.trainNo = this.alarmInfo.trainNo
      const obj = JSON.stringify(this.alarmInfo.obj)
      const params = Object.assign({},{
        page: this.alarmInfo.page,
        size: this.alarmInfo.size,
        obj
      })
      // this.queryData.page = this.currentPage.AL
      // this.queryData.limit = this.pageSize
      const res = await getAlarmPageInfos(params)
      if (res.msg) {
        this.$message.info(res.msg)
      } else {
        this.totalDataLength = res.data.total
        this.rcvTableData = res.data.records
      }
      this.listLoading = false
    },
    async getAllStationNames() {
      this.stationNameOptions = []
      const res = await getStationNames()
      if (res.msg) {
        this.$message.info(res.msg)
      } else {
        this.stationNameOptions = res.data
      }
    },
    reset() {
      this.alarmInfo.startTime =new Date(new Date().getTime() - 3600 * 1000 * 24)
      this.alarmInfo.endTime = new Date(new Date().getTime())
      this.alarmInfo.station = ''
      //this.alarmInfo.workNo = ''
      this.alarmInfo.trainNo = ''
      this.getAllTrainInfos()
    },
    search() {
      this.getAllTrainInfos()
    },
    handleCurrentChange: function(currentPage) {
      this.currentPage = currentPage;
      this.alarmInfo.page = currentPage;
      this.getAllTrainInfos()
    },
    handleSizeChange: function(size) {
      this.pageSize = size
      this.alarmInfo.size = size
      this.getAllTrainInfos()
    }
  }
}